<?
/**
 * VoiceOne Settings/Rules/outgoing
 *
 * Frontend to provide access to the rules system in voiceone.
 * @package voiceone
 * @subpackage  settings_rules
 */
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage,$relBasePath;

  //================ POST & GET ================
  $alert=getAlert();
  // delete ruleset
  if ($_GET["cmd"]=="del") {
    $ret=$asterisk->delRule($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Ruleset deleted")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Error: Ruleset not deleted")."</div>";
    goToMainPage($alert,"./");
  }
  // delete rule
  if ($_GET["cmd"]=="delrule") {
    $ret=$asterisk->delOutgoingRule($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Outgoing Rule deleted")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Error: Outgoing rule not deleted")."</div>";
    goToMainPage($alert,"./");
  }
  // move rule up
  if ($_GET["cmd"]=="up") {
    $ret=$asterisk->moveRule($_GET["id"],1,RULE_OUTGOING,0);
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Outgoing Rule Moved")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Error: Outgoing rule not moved")."</div>";
    goToMainPage($alert,"./?id=".$_GET["id"]);
  } 
  // move rule down
  if ($_GET["cmd"]=="down") {
    $ret=$asterisk->moveRule($_GET["id"],-1,RULE_OUTGOING,0); 
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Outgoing Rule Moved")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Error: Outgoing rule not moved")."</div>";
    goToMainPage($alert,"./?id=".$_GET["id"]);
  }
  // move rule top
  if ($_GET["cmd"]=="top") {
    $ret=$asterisk->moveRule($_GET["id"],1,RULE_OUTGOING,1); 
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Outgoing Rule Moved")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Error: Outgoing rule not moved")."</div>";
    goToMainPage($alert,"./?id=".$_GET["id"]);
  }
  // move rule bottom
  if ($_GET["cmd"]=="bottom") {
    $ret=$asterisk->moveRule($_GET["id"],-1,RULE_OUTGOING,1); 
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Outgoing Rule Moved")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Error: Outgoing rule not moved")."</div>";
    goToMainPage($alert,"./?id=".$_GET["id"]);
  }
  // set flash
  if((isset($_GET["id"]))&&($_GET["id"]!=0)){
    $scriptFade="<script>Fat.fade_element(\"row".$_GET["id"]."\", 30, 500, \"#FFFF33\", \"#FFFFFF\");</script>";
  }
  
  //================ PAGE LOGIC ================
  //Get the rules
  $ret=$asterisk->getRule(0,RULE_OUTGOING);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) 
  foreach($ret as $v) {
    $id=$v["id"];
    $name=$v["name"];
    $description=$v["description"];
    $outgoing=$v["outgoing"];
    $add="<a class=\"admin\" href=\"edit-rule/?rule=$id\">(<!--<img src=\"$relBasePath/public/img/icon_add.gif\" alt=\"Add a new rule\" />-->".T_("Add a new Rule").")</a> ";
    $delete="<a class=\"admin\" href=\"$PHP_SELF?cmd=del&id=$id\" onclick=\"if (confirm('".T_("Are you sure to delete")."\"$name\"?')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a> ";
    $html.="<div class=\"ruleTitle\"><strong>".T_("Rule set n&deg; ")."$id: <a href=\"edit/?id=$id\">$name</a></strong> $add $delete</div>\n";
    if ($description!="") $html.="<div class=\"ruleTitle\"><p>$description</p></div>\n";
    $html.="<table class=\"todolist\" cellspacing=0>";
    $html.="<tr>";
    $html.="<th colspan=\"6\">&nbsp;</th><th>".T_("N&deg;")."</th>";
    $html.="<th>".T_("Time Segment")."</th>";
    $html.="<th>".T_("Destination")."</th>";
    $html.="<th>".T_("Line")."</th>";
    $html.="<th>".T_("Prefix")."</th>";
    $html.="<th>".T_("Action")."</th>";
    $html.="</tr>";
    $count=1;
    if (is_array($outgoing)) 
    //Create rule's row on table
    foreach ($outgoing as $o) { 
      $ruleDisabled=false;       
      $idr=$o["id"];
      $priority=$o["priority"];
      //Extract TIMESEGMENT
      if ($o["timesegment"]=="0") $timesegment="*";
      else {
        $timesegment=$asterisk->getTimeSegment($o["timesegment"]);
        if((is_array($timesegment))&&(is_array($timesegment[0]))&&($timesegment[0]["name"]!="")) $timesegment=$timesegment[0]["name"];
        else {
          $timesegment=T_("deleted timesegment");
          $ruleDisabled=true;
        }
      }
      //Extract DESTINATION
      $destination="";
      //destination is a group
      if ($o["group"]==GROUP_OF_ALL) $destination="*";
      else if ($o["group"]>0) { 
        $icon_group="";
        $g=$asterisk->getGroup($o["group"],0);
        if((is_array($g))&&(is_array($g[0]))&&($g[0]["name"]!="")){
          switch ($g[0]["type"]){   
            case GROUP_OF_EXTENSION:
              $icon_group="<img src=\"$relBasePath/public/img/icon_group.gif\" alt=\"group of extensions\" />";
            break;
            case GROUP_OF_OFFICE:
              $icon_group="<img src=\"$relBasePath/public/img/icon_offices.gif\" alt=\"group of offices\" />";
            break;
            case GROUP_OF_PHONENUMBER:
              $icon_group="<img src=\"$relBasePath/public/img/icon_numbers.gif\" alt=\"group of phonenumbers\" />";
            break;
            case GROUP_OF_APPLICATION:
              $icon_group="<img src=\"$relBasePath/public/img/icon_appls.gif\" alt=\"group of applications\" />";
            break;
          }
          if ($o["group"]<6) $destination="<div style=\"float: left; margin-right: 2px;\">".$icon_group."</div><strong> [".strtoupper(substr($g[0]["name"],1,-1))."]</strong>";
          else $destination="<div style=\"float: left; margin-right: 2px;\">".$icon_group."</div> [".$g[0]["name"]."]";
        }
        else {
          $destination=T_("deleted destination");
          $ruleDisabled=true;
        }
      }
      //destination is an entity
      if ($o["entity"]>0){
        $entityName=$asterisk->getEntityName($o["entity"]);
        if((isset($entityName))&&(entityName!="")){
          $entityName.="<br/>";
          $e=$asterisk->getEntity($o["entity"]);
          $icon_entity="";
          switch ($e[0]["type"]){    
            case EXTENSION:
              $extenArr=getClient($o["entity"],0);
              $icon_entity=getIcon($extenArr[0]["technology"]);
            break;
            case OFFICE:
              $extenArr=getOffice($o["entity"],0);
              $icon_entity=getIcon(IAX2);
            break;
            case PHONENUMBER:
              $icon_entity="<img src=\"$relBasePath/public/img/icon_number.gif\" alt=\"group of phonenumbers\" />";
            break;
            case APPLICATION:
              $icon_entity="<img src=\"$relBasePath/public/img/icon_appl.gif\" alt=\"group of applications\" />";
            break;
          }
          $destination="<div style=\"float: left; margin-right: 2px;\">".$icon_entity."</div>".$entityName;
        }
        else {
          $destination=T_("deleted destination");
          $ruleDisabled=true;
        }
      }
      //Extract RESOURCES (trunk list)
      $trunk=split("\|",$o["resource"]);
      $resource="";
      if(is_array($trunk)){
        foreach ($trunk as $t) {
          if ($t=="0") $resource.="*"."<br>";
          else {
            $tr=$asterisk->getTrunk($t,0);
            if((is_array($tr))&&(is_array($tr[0]))&&($tr[0]["name"]!="")) $resource.=$tr[0]["name"]."<br>";
            else {
              $resource.=T_("deleted line")."<br>";
              $ruleDisabled=true;
            }
          }
        } 
      }
      $g=$asterisk->getGroup($o["group"],0);
      $metag=$asterisk->getMetaGroupFromEntity($o["entity"]);
      if ($o["group"]!=GROUP_OF_PHONENUMBER and $g[0]["type"]!=GROUP_OF_PHONENUMBER and $metag!=GROUP_OF_PHONENUMBER) {
        $resourceClass=" style=\"background: #EAEAEA; color: gray;\" ";
        $resource="N/A";
      } else $resourceClass="";
      // Extract ACTION
      if ($o["action"]==RULE_ALLOW) $action="<span class=\"green\">".T_("ALLOW")."</span>";
      else if ($o["action"]==RULE_DENY) $action="<span class=\"red\">".T_("DENY")."</span>";
      else{ 
        $action="error";
        $ruleDisabled=true;
      }
/////////// DEPRECATED ////////////////
// will be overwrite by filed prefix //
      // Set provider //
      $provider=$o["provider"];
      $provider="no";
//////////////////////////////////////
      // Define BUTTONS
      $button="";
      if ($count==1) $button.="<td class=\"rulebutton\">&nbsp;</td>";
      else $button.="<td class=\"rulebutton\"><a href=\"?cmd=top&id=$idr\"><img src=\"$relBasePath/public/img/icon_top.gif\" alt=\"top\" /></a></td>";
      if ($count==1) $button.="<td class=\"rulebutton\">&nbsp;</td>";
      else $button.="<td class=\"rulebutton\"><a href=\"?cmd=up&id=$idr\"><img src=\"$relBasePath/public/img/icon_up.gif\" alt=\"up\" /></a></td>";
      if ($count==count($outgoing)) $button.="<td class=\"rulebutton\">&nbsp;</td>";
      else $button.="<td class=\"rulebutton\"><a href=\"?cmd=down&id=$idr\"><img src=\"$relBasePath/public/img/icon_down.gif\" alt=\"down\" /></a></td>";
      if ($count==count($outgoing)) $button.="<td class=\"rulebutton\">&nbsp;</td>";
      else $button.="<td class=\"rulebutton\"><a href=\"?cmd=bottom&id=$idr\"><img src=\"$relBasePath/public/img/icon_bottom.gif\" alt=\"bottom\" /></a></td>";
      $button.="<td class=\"rulebutton\"><a href=\"edit-rule/?rule=$id&id=$idr\"><img src=\"$relBasePath/public/img/icon_edit.gif\" alt=\"edit\" /></a></td>";
      $button.="<td class=\"rulebutton\"><a href=\"?cmd=delrule&id=$idr\"><img src=\"$relBasePath/public/img/icon_remove.gif\" alt=\"remove\" /></a></td>";
      // Build TABLE'S ROW
      if($ruleDisabled===true){
        ob_start();
        ?>
        <tr class="firstofgroup rule" id="row<?=$idr?>">
          <?=$button?>
          <td class="disabled"><b><?=$idr?></b></td>
          <td class="disabled"><?=$timesegment?></td>
          <td class="disabled"><?=$destination?></td>
          <td class="disabled"><?=$resource?> </td>
          <td class="disabled"><?=$provider?></td>
          <td class="disabled"><strong><?=$action?></strong></td>
        </tr>
        <?
        $html.=ob_get_contents();
        ob_end_clean();
      }else{
        ob_start();
        ?>
        <tr class="firstofgroup rule" id="row<?=$idr?>">
          <?=$button?>
          <td class="green"><b><?=$idr?></b></td>
          <td><?=$timesegment?></td>
          <td class="highlight"><?=$destination?></td>
          <td <?=$resourceClass?> ><?=$resource?> </td>
          <td><?=$provider?></td>
          <td><strong><?=$action?></strong></td>
        </tr>
        <?
        $html.=ob_get_contents();
        ob_end_clean();
      }
      $count++;
    }
    $html.="</table><br/>\n";
  }
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <?$sel="outgoing";include("../navigation.php");?>

  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=T_("Outgoing calls Rules")?></h1>
    <h3><a href="edit/" class="admin" ><?=T_("New outgoing rule set")?></a></h3>
  </div>
  <div id="basic_form">
    <?=$html?>
  </div>
  
  <?
  echo $scriptFade;
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=T_("Outgoing Rule")?></h1>
          <p><?=T_("This rules will be applied on outgoing calls")?></p>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "rules");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

